package com.ruoyi.ejkgl.ejkwz.service.impl;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ejkgl.ejkwz.domain.Ejkwz;
import com.ruoyi.ejkgl.ejkwz.mapper.EjkwzMapper;
import com.ruoyi.ejkgl.ejkwz.service.IEjkwzService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 二级库物资Service业务层处理
 *
 * @author huacai
 * @date 2024-12-09
 */
@Service
public class EjkwzServiceImpl implements IEjkwzService {
    @Resource
    private EjkwzMapper ejkwzMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询二级库物资
     *
     * @param ejkwzId 二级库物资主键
     * @return 二级库物资
     */
    @Override
    public Ejkwz selectEjkwzByEjkwzId(String ejkwzId) {
        return ejkwzMapper.selectEjkwzByEjkwzId(ejkwzId);
    }

    /**
     * 根据wzxxId查询二级库物资
     *
     * @param wzxxId 二级库物资主键
     * @return
     */
    @Override
    public Ejkwz selectEjkwzByWzxxId(Integer wzxxId, String gq) {
        return ejkwzMapper.selectEjkwzByWzxxId(wzxxId, gq);
    }

    /**
     * 查询二级库物资列表
     *
     * @param ejkwz 二级库物资
     * @return 二级库物资
     */
    @Override
    public List<Ejkwz> selectEjkwzList(Ejkwz ejkwz) {
        return ejkwzMapper.selectEjkwzList(ejkwz);
    }

    /**
     * 查询二级库物资列表存不存在某个wzxx_id(物资信息ID)
     *
     * @param wzxxId
     * @return
     */
    @Override
    public String selectCzEjkwzId(Integer wzxxId, String gq) {
        return ejkwzMapper.selectCzEjkwzId(wzxxId, gq);
    }

    /**
     * 新增二级库物资
     *
     * @param ejkwz 二级库物资
     * @return 结果
     */
    @Override
    public int insertEjkwz(Ejkwz ejkwz) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ejkwz.setUserId(Math.toIntExact(loginUser.getUserId())); //用户ID
        ejkwz.setDeptId(Math.toIntExact(loginUser.getDeptId())); //部门ID
        ejkwz.setCreateBy(loginUser.getUsername()); //创建人
        ejkwz.setIdWhere("存在");
        ejkwz.setCreateTime(DateUtils.getNowDate());
        return ejkwzMapper.insertEjkwz(ejkwz);
    }

    /**
     * 批量新增二级库物资
     *
     * @param ejkwzs 二级库物资List
     * @return 结果
     */
    @Override
    public int batchInsertEjkwz(List<Ejkwz> ejkwzs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(ejkwzs)) {
            try {
                for (int i = 0; i < ejkwzs.size(); i++) {
                    int row = ejkwzMapper.insertEjkwz(ejkwzs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == ejkwzs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改二级库物资
     *
     * @param ejkwz 二级库物资
     * @return 结果
     */
    @Override
    @Transactional
    public int updateEjkwz(Ejkwz ejkwz) {
        ejkwz.setUpdateTime(DateUtils.getNowDate());
        return ejkwzMapper.updateEjkwz(ejkwz);
    }

    /**
     * 批量删除二级库物资
     *
     * @param ejkwzIds 需要删除的二级库物资主键
     * @return 结果
     */
    @Override
    public int deleteEjkwzByEjkwzIds(String[] ejkwzIds) {
        return ejkwzMapper.deleteEjkwzByEjkwzIds(ejkwzIds);
    }

    /**
     * 删除二级库物资信息
     *
     * @param ejkwzId 二级库物资主键
     * @return 结果
     */
    @Override
    public int deleteEjkwzByEjkwzId(String ejkwzId) {
        return ejkwzMapper.deleteEjkwzByEjkwzId(ejkwzId);
    }
}
